﻿using System;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace TheOliver.Helper
{
    public class Byte2ImageConverter : IValueConverter
    {

        public Byte2ImageConverter()
        {
        }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return null;
            }

            byte[] bytes;
            bytes = value as byte[];

            if (bytes == null)
            {
                return null;
            }

            MemoryStream stream = new MemoryStream(bytes);
            BitmapImage b = new BitmapImage();
            b.SetSource(stream);
            return b;
        }

        public object ConvertBack(object value, Type targetType, 
            object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }

        #endregion

    }
}
